/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package triploa;
import org.j4me.bluetoothgps.*;
/**
 *
 * @author Edoardo
 */
public class GPSSearch implements Runnable{
    MainMIDlet midlet=null;
    GPSSearch(MainMIDlet m)
    {
        midlet=m;
    }
    public void run() {
        try{
        // Create our location criteria assuming the hosting device has it.
        Criteria criteria = new Criteria();
        criteria.setHorizontalAccuracy( 1 );  // +/- 1 meter 68.27% of the time
        // [Set other criteria here]
 
        // See if anything matches on the local device.
        LocationProvider provider = LocationProvider.getInstance( criteria );
 
        // Is there a LBS on the local device?
        if ( provider == null )  // then nothing on local device
        {
                // Get the Bluetooth devices within 10 meters that are accepting
                // connections.  This can take 10 seconds or more.
                String[][] remoteDevices = LocationProvider.discoverBluetoothDevices();
 
                if ( remoteDevices == null )
                {
                        return;
                }
 
                // Show the user a list of devices by the human readable names.
                String[] deviceNames = new String[remoteDevices.length];
                
                for ( int i = 0; i < remoteDevices.length; i++ )
                {
                        deviceNames[i] = remoteDevices[i][0];
                        System.out.println(deviceNames[i]);
                }
 
                // [Show the list here and set an integer deviceIndex to it]
 
                // Get the Bluetooth address of the device.
                String address = remoteDevices[0][1];
 
                // [Store the device's name and address for next time]

                // See if the remote device will work.
                criteria.setRemoteDeviceAddress( address );
                provider = LocationProvider.getInstance( criteria );
                provider.setLocationListener(midlet, -1, -1, -1);
        }
        
        
        }catch(Exception e)
        {
            System.out.println(e.getMessage());

        }
    }

}
